package com.liangtian.mdm.base.manager;

import com.liangtian.mdm.system.button.model.SysPermissionButton;
import com.liangtian.mdm.system.permission.model.SysPermission;
import com.liangtian.mdm.system.permission.model.SysRole;
import com.liangtian.mdm.system.permission.service.SysPermissionService;
import com.liangtian.mdm.system.permission.service.SysRoleService;
import com.liangtian.mdm.system.user.model.SysUser;
import com.liangtian.mdm.system.user.service.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @author Qi
 */
@Service
public class UserManager {

    @Autowired
    private SysRoleService sysRoleService;
    @Autowired
    private SysUserService sysUserService;
    @Autowired
    SysPermissionService sysPermissionService;

    /**
     * 通过用户名获取用户基本信息
     *
     * @param userAccount 用户名
     * @return 用户基本信息
     */
    public SysUser getUser(String userAccount) {
        return sysUserService.queryUserByUserAccount(userAccount);
    }

    /**
     * 通过用户名获取用户角色集合
     *
     * @param userAccount 用户名
     * @return 角色集合
     */
    public Set<String> getUserRoles(String userAccount) {
        List<SysRole> roleList = this.sysRoleService.getRoleListByUserAccount(userAccount);
        return roleList.stream().map(SysRole::getRoleName).collect(Collectors.toSet());
    }

    /**
     * 通过用户名获取用户权限集合
     *
     * @param userAccount 用户名
     * @return 权限集合
     */
    public Set<String> getUserPermissions(String userAccount) {
        List<SysPermission> permissionList = sysPermissionService.queryPermissionsByUserAccount(userAccount);
        List<SysPermissionButton> buttonPermissionList = sysPermissionService.queryButtonPermissionsByUserAccount(userAccount);
        Set<String> collect1 = permissionList.stream().map(SysPermission::getPermissionCode).collect(Collectors.toSet());
        Set<String> collect2 = buttonPermissionList.stream().map(SysPermissionButton::getShiroButton).collect(Collectors.toSet());
        collect1.addAll(collect2);
        return collect1;
    }
}
